﻿using System;
using System.Drawing;
using System.Windows.Forms;
using AiFrame.InterfaceLib.Windows.Controls;

namespace AiFrame.Windows.Controls.DataNavigationBar
{
    public partial class FrameDataNavigationBarButton : UserControl, IDataNavigationBarButton
    {
        private Color _backColor = Color.White;
        private Color _hoverColor = Color.FromArgb(215, 215, 215);
        private string _caption;
        private Image _image;
        private Font _captionFont = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
        private OnDataNavBarClickProcedure _clickMethod;
        private object _saveObject;
        private int _positionIndex;

        public FrameDataNavigationBarButton()
        {
            Init();
        }

        public FrameDataNavigationBarButton(string name, string caption,
            Image normalImage, Image hoveredImage, OnDataNavBarClickProcedure proc,
            IDataNavigationBarTheme theme) :base()
        {
            Init();

            _caption = caption;
            this.Name = name;
            _image = normalImage;
            _clickMethod = proc;
        }

        public FrameDataNavigationBarButton(string name, string caption,
            Image normalImage, Image hoveredImage, OnDataNavBarClickProcedure proc,
            IDataNavigationBarTheme theme, object saveObject, int positionIndex) :base()
        {
            Init();

            _caption = caption;
            this.Name = name;
            _image = normalImage;
            _clickMethod = proc;
            _saveObject = saveObject;
            _positionIndex = positionIndex;
        }

        private void Init()
        {
            InitializeComponent();
            BackColor = _backColor;

        }

        public int PositionIndex
        {
            get{ return _positionIndex; }
        }

        private void FrameDataNavigationBarButton_MouseLeave(object sender, EventArgs e)
        {
            BackColor = _backColor;
            BorderStyle = System.Windows.Forms.BorderStyle.None;
        }

        private void FrameDataNavigationBarButton_MouseEnter(object sender, EventArgs e)
        {
            BackColor = _hoverColor;
            BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        }

        private void FrameDataNavigationBarButton_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawString(_caption, _captionFont, new SolidBrush(Color.FromArgb(40, 40, 40)), 45, 12);
            if (_image != null)
                e.Graphics.DrawImage(_image, 5, 5, _image.Width, _image.Height);
        }

        private void FrameDataNavigationBarButton_Click(object sender, EventArgs e)
        {
            /* Die OnClick-Methode aufrufen, als Sender diese Klasse übergeben,
            * damit der Empfänger daraus ein IDataNavigationBarButton casten kann */
            _clickMethod(this, e);
        }

        #region Implementation of IDataNavigationBarButton

        public object GetSaveObject()
        {
            return _saveObject;
        }

        #endregion
    }
}
